import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/config/constant.dart';
import 'package:kkhousekeep/page/home/convenient/repair/rating_bar_dialog.dart';
import 'package:kkhousekeep/page/home/convenient/repair/repair_add_controller.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/custom_button.dart';
import 'package:kkhousekeep/widget/kk_default_tab_controller.dart';

import '../../../../models/repair_order_entity.dart';
import 'repair_add_page.dart';
import 'repair_home_controller.dart';

class RepairHomePage extends StatelessWidget {
  const RepairHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: RepairHomeController(),
      builder: (repairHomeController) => Scaffold(
        appBar: CustomAppBar(
          "物业报修",
          isCenterTitle: true,
        ),
        body: KKDefaultTabController(
          onTabListener: (tabController) =>
              repairHomeController.performClickTab(tabController.index),
          length: 2,
          builder: (tabController) => SingleChildScrollView(
            child: Stack(
              alignment: Alignment.center,
              children: [
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    _builderTabBarWidget(repairHomeController, tabController),
                    _builderTabBarViewWidget(
                        repairHomeController, tabController),
                  ],
                ),
                Positioned(
                  bottom: 60.h,
                  child: CustomButton(
                    onPressed: () =>
                        Get.to(() => const RepairAddPage())?.then((value) {
                      if (value == Constant.resultOK) {
                        DialogUtil.showNetworkSucceed();
                        repairHomeController.loadNetworkData();
                      }
                    }),
                    text: "+ 新增报修",
                    width: 420.w,
                    height: 90.h,
                    isAsh: false,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _builderTabBarWidget(
    RepairHomeController repairHomeController,
    TabController tabController,
  ) {
    return Obx(() => TabBar(
          isScrollable: false,
          controller: tabController,
          padding: EdgeInsets.zero,
          labelPadding: EdgeInsets.zero,
          indicator: const BoxDecoration(),
          tabs: repairHomeController.tabs.map((model) {
            bool isEnable = model.id == repairHomeController.tabId.value;
            return Tab(
              child: Container(
                width: double.infinity,
                height: 88.h,
                color: Colors.white,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Positioned(
                      child: Text(
                        model.title,
                        style: TextStyle(
                          fontSize: 32.sp,
                          color: isEnable
                              ? const Color(0xFF1774E0)
                              : const Color(0xFF999999),
                        ),
                      ),
                    ),
                    Positioned(
                      bottom: 2.h,
                      child: Container(
                        width: 48.w,
                        height: 4.h,
                        decoration: BoxDecoration(
                          gradient: isEnable
                              ? const LinearGradient(
                                  colors: [
                                    Color(0xFF0A4EF1),
                                    Color(0xFF7AE0E7),
                                  ],
                                )
                              : null,
                          borderRadius: BorderRadius.circular(2.h),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            );
          }).toList(),
        ));
  }

  Widget _builderTabBarViewWidget(
      RepairHomeController repairHomeController, TabController tabController) {
    return Obx(() => Container(
          padding: EdgeInsets.all(32.w),
          height: 1360.h,
          child: TabBarView(
              controller: tabController,
              children: repairHomeController.tabs
                  .map(
                    (tab) => tab.id == 0
                        ? _builderPendingListViewWidget(repairHomeController)
                        : _builderFinishListViewWidget(repairHomeController),
                  )
                  .toList()),
        ));
  }

  Widget _builderPendingListViewWidget(
      RepairHomeController repairHomeController) {
    var dataList = repairHomeController.pendingList;
    if (dataList.isEmpty) {
      return _builderEmptyWidget();
    }

    var leftTextStyle = TextStyle(
      fontSize: 28.sp,
      color: const Color(0xFF6D6D6D),
    );
    var rightTextStyle = TextStyle(
      fontSize: 28.sp,
      color: const Color(0xFF333333),
    );

    return ListView.builder(
      itemCount: dataList.length,
      itemBuilder: (context, index) {
        RepairOrderObj model = dataList[index];
        bool isProcessing = model.state == 'processing';
        String progressTypeImage = isProcessing
            ? "flag_notification_passed.png"
            : "flag_notification_pending.png";
        String urgent = UrgencyType.getNameById(model.urgency);
        bool isLastOne = index == dataList.length - 1;
        return InkWell(
          onTap: () => repairHomeController.performClickItem(model.id),
          child: Container(
            margin: EdgeInsets.only(bottom: isLastOne ? 176.h : 16.h),
            height: 278.h,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(16.w),
            ),
            child: Stack(
              children: [
                ///时间
                Positioned(
                    left: 24.w,
                    top: 10.h,
                    child: Text(
                      model.cTime,
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: const Color(0XFF999999),
                      ),
                    )),

                ///进度状态
                Positioned(
                    top: 0,
                    right: 0.w,
                    width: 180.w,
                    height: 56.h,
                    child: Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("assets/images/$progressTypeImage"),
                          fit: BoxFit.fill,
                        ),
                      ),
                      child: Container(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(right: 32.w),
                        child: Text(
                          isProcessing ? "处理中" : "待处理",
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: isProcessing
                                ? const Color(0XFF55D4A5)
                                : const Color(0XFFDD5045),
                          ),
                        ),
                      ),
                    )),

                ///分割线
                Positioned(
                    top: 56.h,
                    left: 0,
                    right: 0,
                    height: 2.h,
                    child: Container(
                      color: const Color(0XFFF0F2F7),
                    )),

                Positioned(
                    left: 24.w,
                    top: 84.h,
                    child: Text(
                      "报修地址",
                      style: leftTextStyle,
                    )),

                Positioned(
                    left: 24.w,
                    top: 140.h,
                    child: Text(
                      "故障类型",
                      style: leftTextStyle,
                    )),

                Positioned(
                    left: 24.w,
                    bottom: 30.h,
                    child: Text(
                      "紧急程度",
                      style: leftTextStyle,
                    )),

                ///下面是具体值
                Positioned(
                    right: 32.w,
                    top: 84.h,
                    child: Text(
                      model.address,
                      style: rightTextStyle,
                    )),

                Positioned(
                    right: 32.w,
                    top: 140.h,
                    child: Text(
                      model.fatalType,
                      style: rightTextStyle,
                    )),

                Positioned(
                    right: 32.w,
                    bottom: 30.h,
                    child: Text(
                      urgent,
                      style: model.urgency == UrgencyType.veryUrgent.id
                          ? TextStyle(
                              fontSize: 28.sp, color: const Color(0XFFDD5045))
                          : rightTextStyle,
                    )),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _builderFinishListViewWidget(
      RepairHomeController repairHomeController) {
    var dataList = repairHomeController.finishList;

    if (dataList.isEmpty) {
      return _builderEmptyWidget();
    }

    var leftTextStyle = TextStyle(
      fontSize: 28.sp,
      color: const Color(0xFF6D6D6D),
    );
    var rightTextStyle = TextStyle(
      fontSize: 28.sp,
      color: const Color(0xFF333333),
    );

    return ListView.builder(
      itemCount: dataList.length,
      itemBuilder: (context, index) {
        var model = dataList[index];

        bool isEvaluated = model.state == 'evaluated';
        String urgent = UrgencyType.getNameById(model.urgency);
        bool isLastOne = index == dataList.length - 1;

        return InkWell(
          onTap: () => repairHomeController.performClickItem(model.id),
          child: Container(
            margin: EdgeInsets.only(bottom: isLastOne ? 176.h : 16.h),
            height: 338.h,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(16.w),
            ),
            child: Stack(
              children: [
                ///时间
                Positioned(
                    left: 24.w,
                    top: 10.h,
                    child: Text(
                      model.cTime,
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: const Color(0XFF999999),
                      ),
                    )),

                ///报修地址value
                Positioned(
                    right: 32.w,
                    top: 76.h,
                    child: Text(
                      model.address,
                      style: rightTextStyle,
                    )),

                ///报修类型value
                Positioned(
                    right: 32.w,
                    top: 132.h,
                    child: Text(
                      model.fatalType,
                      style: leftTextStyle,
                    )),

                ///紧急程度value
                Positioned(
                    right: 32.w,
                    top: 188.h,
                    child: Text(
                      urgent,
                      style: model.urgency == UrgencyType.veryUrgent.id
                          ? TextStyle(
                              fontSize: 28.sp, color: const Color(0XFFDD5045))
                          : rightTextStyle,
                    )),

                ///立即评价 or 查看评价
                Positioned(
                  right: 256.w,
                  bottom: 16.h,
                  width: 208.w,
                  height: 56.h,
                  child: InkWell(
                    onTap: () {
                      SmartDialog.show(
                        builder: (context) => RatingBarDialog(
                          orderId: model.id,
                          controller: repairHomeController,
                          isEvaluated: isEvaluated,
                          responseSpeed: model.responseSpeed.toDouble(),
                          serviceAttitude: model.serviceAttitude.toDouble(),
                          constructionQuality:
                              model.constructionQuality.toDouble(),
                        ),
                      ).whenComplete(
                        () => repairHomeController.loadNetworkData(),
                      );
                    },
                    child: Container(
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(28.w),
                        border: Border.fromBorderSide(
                          BorderSide(
                            color: isEvaluated
                                ? const Color(0XFF6D6D6D)
                                : const Color(0XFF1774E0),
                            width: 2.w,
                          ),
                        ),
                      ),
                      child: Text(
                        isEvaluated ? "查看评价" : "立即评价",
                        style: TextStyle(
                          fontSize: 32.sp,
                          color: isEvaluated
                              ? const Color(0XFF6D6D6D)
                              : const Color(0XFF1774E0),
                        ),
                      ),
                    ),
                  ),
                ),

                ///处理结果
                Positioned(
                  right: 32.w,
                  bottom: 16.h,
                  width: 208.w,
                  height: 56.h,
                  child: InkWell(
                    onTap: () => repairHomeController
                        .performClickGotoDisposeResult(model.id),
                    child: Container(
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(28.w),
                        border: Border.fromBorderSide(
                          BorderSide(
                            color: const Color(0XFF6D6D6D),
                            width: 2.w,
                          ),
                        ),
                      ),
                      child: Text(
                        "处理结果",
                        style: TextStyle(
                          fontSize: 32.sp,
                          color: const Color(0XFF6D6D6D),
                        ),
                      ),
                    ),
                  ),
                ),

                ///下面几项都是常量值
                ///分割线
                Positioned(
                    bottom: 88.h,
                    left: 0,
                    right: 0,
                    height: 2.h,
                    child: Container(
                      color: const Color(0XFFF0F2F7),
                    )),

                Positioned(
                    left: 24.w,
                    top: 76.h,
                    child: Text(
                      "报修地址",
                      style: leftTextStyle,
                    )),

                Positioned(
                    left: 24.w,
                    top: 132.h,
                    child: Text(
                      "故障类型",
                      style: leftTextStyle,
                    )),

                Positioned(
                    left: 24.w,
                    top: 188.h,
                    child: Text(
                      "紧急程度",
                      style: leftTextStyle,
                    )),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _builderEmptyWidget() {
    return Container(
      margin: EdgeInsets.only(top: 292.h),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Image.asset(
            "assets/images/home_convenient_repair_empty.png",
            width: 288.w,
            height: 370.h,
          ),
          Padding(
            padding: EdgeInsets.only(top: 8.h),
            child: Text(
              "没有找到你的维修申请",
              style: TextStyle(
                fontSize: 28.sp,
                color: const Color(0XFF999999),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
